package com.sopaths.utility.scheduler;

import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDataMap;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.FactoryBean;

public class CronTriggerFactoryBean implements FactoryBean<Trigger> {

	private String group;

	private String name;

	private String cronExpression;

	private Map<String, Object> jobDataMap;

	@Override
	public Trigger getObject() throws Exception {
		Trigger trigger = TriggerBuilder.newTrigger()
				.withIdentity(name, group)
				.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
				.build();
		JobDataMap jobDataMap = trigger.getJobDataMap();
		if (this.jobDataMap != null) {
			for (String key : this.jobDataMap.keySet()) {
				jobDataMap.put(key, this.jobDataMap.get(key));
			}
		}
		return trigger;
	}

	@Override
	public Class<?> getObjectType() {
		return Trigger.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public Map<String, Object> getJobDataMap() {
		return jobDataMap;
	}

	public void setJobDataMap(Map<String, Object> jobDataMap) {
		this.jobDataMap = jobDataMap;
	}

}
